package com.ailanyin.admin.service.impl;

import com.ailanyin.admin.service.ExceptionLogService;
import com.ailanyin.mapper.ExceptionLogMapper;
import com.ailanyin.model.ExceptionLog;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author ailanyin
 * @version 1.0
 * @since 2021/11/2 0002 下午 17:23
 */
@Service
public class ExceptionLogServiceImpl implements ExceptionLogService {

    @Resource
    private ExceptionLogMapper logMapper;

    /**
     * 查询异常信息列表
     *
     * @param exceptionLog exceptionLog
     * @return list
     */
    @Override
    public List<ExceptionLog> selectExceptionLog(ExceptionLog exceptionLog) {
        return logMapper.selectListByParam(exceptionLog);
    }

    /**
     * 新增异常
     *
     * @param exceptionLog exceptionLog
     */
    @Override
    public void insertExceptionLog(ExceptionLog exceptionLog) {
        exceptionLog.setCreateTime(new Date());
        logMapper.insertExceptionLog(exceptionLog);
    }

    /**
     * 新增异常
     *
     * @param exception exception
     */
    @Override
    public void insertExceptionLog(Exception exception) {
        ExceptionLog log = new ExceptionLog();
        log.setExceptionReason(exception.getMessage());

        StringBuilder builder = new StringBuilder();
        if (exception.getStackTrace().length > 0) {
            for (StackTraceElement traceElement : exception.getStackTrace()) {
                builder.append(traceElement).append("<br>");
            }
        }
        log.setDetailInfo(builder.toString());
        insertExceptionLog(log);
    }

    /**
     * 批量删除异常日志
     *
     * @param logIds ids
     */
    @Override
    public void removeExceptionLog(List<String> logIds) {
        logMapper.deleteBatchIds(logIds);

    }
}
